﻿Public Class ErrorHandler

    Private exException As System.Exception
    Public Property Exception() As System.Exception
        Get
            Return exException
        End Get
        Set(ByVal value As System.Exception)
            exException = value
        End Set
    End Property

    Private sMessageText As String
    Public Property MessageText() As String
        Get
            Return sMessageText
        End Get
        Set(ByVal value As String)
            sMessageText = value
        End Set
    End Property


    Private sInnerException As String
    Public Property InnerException() As String
        Get
            Return sInnerException
        End Get
        Set(ByVal value As String)
            sInnerException = value
        End Set
    End Property

    Private sErrorLocation As String
    Public Property ErrorLocation() As String
        Get
            Return sErrorLocation
        End Get
        Set(ByVal value As String)
            sErrorLocation = value
        End Set
    End Property



    Sub New()

    End Sub
    Sub New(ByVal ErrorException As System.Exception)
        Me.Exception = ErrorException
        Me.MessageText = Me.Exception.Message.ToString
        If Not Exception.InnerException Is Nothing Then Me.InnerException = Me.Exception.InnerException.ToString
    End Sub

    Sub ShowMsgbox(ByVal ErrorException As System.Exception, ByVal myErrorLocation As String)
        'shows messages no matter the setting - for configuration errors
        Me.Exception = ErrorException
        Me.ErrorLocation = myErrorLocation
        Me.MakeErrorString()
        MsgBox(Me.MessageText, MsgBoxStyle.OkOnly, "Error")
    End Sub

    Sub LogError(ByVal ErrorException As System.Exception, ByVal myErrorLocation As String)
        Me.Exception = ErrorException
        Me.ErrorLocation = myErrorLocation
        Me.MakeErrorString()
        Logger.ErrorLog(Me.MessageText)
    End Sub

    Private Sub MakeErrorString()
        If Me.Exception.InnerException Is Nothing Then
            Me.sMessageText = "Error in " & Me.ErrorLocation & ": " & Me.Exception.ToString & "-" & Me.Exception.Message
        Else
            Me.sMessageText = "Error in " & Me.ErrorLocation & ": " & Me.Exception.ToString & "-" & Me.Exception.Message & "-" & Me.InnerException
        End If
    End Sub
End Class
